<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发布评论</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        ul{
            list-style: none;
        }
        #box{
            width: 1000px;
            border: 1px solid #ccc;
            margin: 100px auto;
            padding: 20px;
        }
        #comment{
            width: 80%;
            padding: 10px 10px;
            font-size: 20px;
            outline: none;
        }
        .box-top{
            margin-bottom: 20px;
        }
        #comment-content li{
            border-bottom: 1px dashed #ccc;
            width: 800px;
            color: red;
            line-height: 45px;
        }
        #comment-content li a{
            float: right;
        }
    </style>
</head>
<body>
    <div id="box">
        <div class="box-top">
            <textarea name="" id="comment" cols="80" rows="10" placeholder="请输入您的评论"></textarea>
            <button id="btn"> 发布</button>
        </div>
        <ul id="comment-content">
        </ul>
    </div>
    <script>
        window.onload = function(){
            $("btn").onclick = function(){
                var commentValue = $("comment").value;
                if(commentValue.length === 0){
                    alert("您输入的内容为空");
                    return;
                }
                var newLi = document.createElement('li');
                newLi.innerHTML = commentValue+'<a href="javascript:void(0);">删除</a>';
               //$("comment-content").appendChild(newLi);
               $("comment-content").insertBefore(newLi,$("comment-content").children[0]);
               $('comment').value = '';
                var items = document.getElementsByTagName('a');
                for(var i = 0;i < items.length;i++){
                    items[i].onclick = function(){
                        this.parentNode.remove();
                    }
                }
        }
        function $(id){
            return typeof id==="string" ? document.getElementById(id):null;
        }
    }      
    </script>
</body>
</html>